/**
 * Monkey World is an environment where a monkey agent can stole a bunch of bananas and go home.
 * Copyright (C) 2011 Deep Blue Team <see the team details file>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package monkeyproject.gui;


import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


import javax.swing.JPanel;

import monkeyproject.environment.Laboratory;


/**
 *
 * @author Deep Blue Team
 */
public class BottomPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private Image monkey;
	private Image box;
	private Image bananas;
	private Laboratory lab;
	private Image env;

	private int size = 60;
	private int topPosition = 200;
	private int bottomPosition = 350;
	private boolean edit;
	private int choice;	
	private int monkeyPosition=240;
	private int boxPosition=350;
	private int bananaPosition=200;

	public BottomPanel( int c, final Laboratory l ) 
	{
		this.choice = c;
		lab = l;
		setEditable(true);

		Toolkit t = Toolkit.getDefaultToolkit();
		monkey = t.getImage( "src/resources/monkey.gif" );
		box = t.getImage( "src/resources/box.gif" );
		bananas = t.getImage("src/resources/banana.gif" );
		env = t.getImage( "src/resources/env.jpg" );
		
		MediaTracker mt = new MediaTracker( this );
		mt.addImage( monkey, 0 );
		mt.addImage( box, 1 );
		mt.addImage( bananas, 2 );
	
		try 
		{
			mt.waitForID( 0 );
			mt.waitForID( 1 );
			mt.waitForID( 2 );
		} 
		catch ( InterruptedException e ) 
		{
			e.printStackTrace();
		}
		this.setFocusable( true );
		this.addKeyListener( new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent arg0) {				
			}
			
			@Override
			public void keyReleased(KeyEvent arg0) {
			}
			
			@Override
			public void keyPressed(KeyEvent arg0) {
				l.setInvisible( true );
			}
		});

		this.addMouseListener( new MouseListener() 
		{

			@Override
			public void mouseReleased( MouseEvent e ) 
			{
			}

			@Override
			public void mousePressed( MouseEvent e ) 
			{
			}

			@Override
			public void mouseExited( MouseEvent e ) 
			{
			}

			@Override
			public void mouseEntered( MouseEvent e )
			{
			}

			@Override
			public void mouseClicked( MouseEvent e ) 
			{
				int x = e.getX();
				int y = e.getY();
				int i = (x - 100) / 60;
				if( i >= 0 && i < 10 ) 
				{
					if( y > 200 && y < 260 ) 
					{
						if ( isEditable() || choice == 2 )
							lab.setBananasBunch( i );
					} 
					else if( y > 320 && y < 380 ) 
					{
						if ( isEditable() )
							lab.setBox( i );
					}
					else if( y > 440 && y < 500 )
					{
						if( isEditable() && i != lab.getBox() )
							lab.setHome( i );						
					}
					repaint();
				}
			}
		});
	}
	
	@Override
	public void paintComponent( Graphics graphics ) 
	{	
		super.paintComponent( graphics );
		
		graphics.drawImage(env, 0, 0,this);
		graphics.drawImage( box, lab.getBox() * size + 60, boxPosition , null );
		graphics.drawString( "Performance Measure: " + lab.getPerformanceMeasure(), 20, 30 );
		
		
	
		  for( int i = 0; i < 10; i++ )
          {
                  graphics.drawRect( i * size + 100, topPosition, 10, 10 );
                  graphics.drawRect(i * size + 100, ( topPosition+150 ), 10, 10 );
                  graphics.drawRect(i * size + 100, ( topPosition+250 ), 10, 10 );
          }       
                
          
		  
		if( !lab.isGrabbed() )
			graphics.drawImage( bananas, lab.getBananasBunch() * size + 60, bananaPosition , null );
		

		if( lab.isOnTheBox() )
		{
			graphics.drawImage( monkey, lab.getMonkey() * size + 50, monkeyPosition, null );
			
		} 
		else 
		{
			if( lab.isMonkeyAtHome() )
			{				
				graphics.drawImage( monkey, lab.getMonkey() * size + 50, bottomPosition + 70, null );
			}
			else
			{			
				if( lab.isGrabbed() || ( !lab.isInvisible() ) )					
					graphics.drawImage( monkey, lab.getMonkey() * size + 50, bottomPosition + 10, null );
			}
		}
		
	}

	public void setEditable( boolean edit ) 
	{
		this.edit = edit;
	}

	public boolean isEditable() 
	{
		return edit;
	}
}